package org.apache.ibatis.reflection;

import org.apache.ibatis.reflection.invoker.GetFieldInvoker;
import org.apache.ibatis.reflection.invoker.Invoker;
import org.apache.ibatis.reflection.invoker.MethodInvoker;
import org.apache.ibatis.reflection.invoker.SetFieldInvoker;
import org.apache.ibatis.reflection.property.PropertyNamer;

import java.lang.reflect.*;
import java.util.*;
import java.util.Map.Entry;

/**
 * This class represents a cached set of class definition information that
 * allows for easy mapping between property names and getter/setter methods.
 * 这个类的用途主要是是通过反射获取目标类的 getter 方法及其返回值类型，
 * setter 方法及其参数值类型等元信息。并将获取到的元信息缓存到相应的集合中，供后续使用。
 * <p>
 * 缓存了反射操作需要使用的类的元信息
 * 使得属性名称和GETTER/SETTER方法之间的映射变简单
 * <p>
 * JavaBean 规范：
 * 类中定义的成员变量也称为"字段"，属性则是通过getter/setter方法得到的，属性只与类中的方法有关，
 * 与是否存在对应成员变量没有关系。例如，存在getA()方法和setA(String)方法，无论类中是否
 * 定义了字段String a, 我们都认为该类中存在属性a !
 *
 * @author Clinton Begin
 */
public class Reflector {

    /**
     * 对应的Class类型
     */
    private final Class<?> type;
    /**
     * 可读属性集合 存在相应getter方法
     */
    private final String[] readablePropertyNames;
    /**
     * 可写属性集合 存在相应setter方法
     */
    private final String[] writeablePropertyNames;
    /**
     * 记录了属性对应的setter方法
     * key是属性名，value是Invoker对象(对setter方法对应Method对象的封装)
     */
    private final Map<String, Invoker> setMethods = new HashMap<String, Invoker>();
    /**
     * 记录了属性对应的getter方法
     * key是属性名，value是Invoker对象(对getter方法对应Method对象的封装)
     */
    private final Map<String, Invoker> getMethods = new HashMap<String, Invoker>();
    /**
     * 记录了属性对应的setter方法的参数值类型
     */
    private final Map<String, Class<?>> setTypes = new HashMap<String, Class<?>>();
    /**
     * 记录了属性对应的getter方法的返回值类型
     */
    private final Map<String, Class<?>> getTypes = new HashMap<String, Class<?>>();
    /**
     * 默认构造方法
     */
    private Constructor<?> defaultConstructor;

    /**
     * 所有属性名称集合
     * key是属性名的大写，value是属性名
     */
    private Map<String, String> caseInsensitivePropertyMap = new HashMap<String, String>();

    public Reflector(Class<?> clazz) {
        type = clazz;
        // 查找clazz 的默认构造方法（无参构造方法），具体实现是通过反射遍历所有构造方法
        addDefaultConstructor(clazz);
        // 处理clazz 中的getter 方法，填充getMethods 集合和getTypes 集合
        addGetMethods(clazz);
        // 处理clazz 中的setter 方法，填充setMethods 集合和setTypes 集合
        addSetMethods(clazz);
        // 解析属性字段，处理没有getter / setter 方法的字段
        addFields(clazz);
        // 根据getMethods/setMethods 集合，初始化可读／写属性的名称集合
        readablePropertyNames = getMethods.keySet().toArray(new String[getMethods.keySet().size()]);
        writeablePropertyNames = setMethods.keySet().toArray(new String[setMethods.keySet().size()]);

        // 初始化caseinsensitivePropertyMap 集合，其中记录了所有大写格式的属性名称
        for (String propName : readablePropertyNames) {
            caseInsensitivePropertyMap.put(propName.toUpperCase(Locale.ENGLISH), propName);
        }
        // 将所有属性名的大写形式作为键，属性名作为值，存入到 caseInsensitivePropertyMap 中
        for (String propName : writeablePropertyNames) {
            caseInsensitivePropertyMap.put(propName.toUpperCase(Locale.ENGLISH), propName);
        }
    }

    private void addDefaultConstructor(Class<?> clazz) {
        Constructor<?>[] consts = clazz.getDeclaredConstructors();
        for (Constructor<?> constructor : consts) {
            // 无参构造函数
            if (constructor.getParameterTypes().length == 0) {
                if (canAccessPrivateMethods()) {
                    try {
                        constructor.setAccessible(true);
                    } catch (Exception e) {
                        // Ignored. This is only a final precaution, nothing we can do.
                    }
                }
                if (constructor.isAccessible()) {
                    this.defaultConstructor = constructor;
                }
            }
        }
    }

    private void addGetMethods(Class<?> cls) {
        Map<String, List<Method>> conflictingGetters = new HashMap<String, List<Method>>();
        // 获取当前类，接口，以及父类中的方法。
        Method[] methods = getClassMethods(cls);
        for (Method method : methods) {
            // getter 方法不应该有参数，若存在参数，则忽略当前方法
            if (method.getParameterTypes().length > 0) {
                continue;
            }
            String name = method.getName();
            // 过滤出以 get 或 is 开头的方法
            boolean getFlag = name.startsWith("get") && name.length() > 3;
            boolean isFlag = name.startsWith("is") && name.length() > 2;
            if (getFlag || isFlag) {
                // 是get方法
                // 将 getXXX 或 isXXX 等方法名转成相应的属性，比如 getName -> name
                name = PropertyNamer.methodToProperty(name);
                /*
                 * 将冲突的方法添加到 conflictingGetters 中。
                 * 考虑这样一种情况：
                 * getTitle 和 isTitle 两个方法经过 methodToProperty 处理，
                 * 均得到 name = title，这会导致冲突。
                 *
                 * 对于冲突的方法，这里先统一起存起来，后续再解决冲突
                 */
                addMethodConflict(conflictingGetters, name, method);
            }
        }
        // 解决 getter 冲突
        resolveGetterConflicts(conflictingGetters);
    }

    /**
     * 添加属性名和方法对象到冲突集合中
     */
    private void addMethodConflict(Map<String, List<Method>> conflictingMethods, String name, Method method) {
        List<Method> list = conflictingMethods.get(name);
        if (list == null) {
            list = new ArrayList<Method>();
            conflictingMethods.put(name, list);
        }
        list.add(method);
    }

    /**
     * 解决冲突，相关规则如下：
     * <p>
     * 1.冲突方法的返回值类型具有继承关系，子类返回值对应的方法被认为是更合适的选择
     * 2.冲突方法的返回值类型相同，如果返回值类型为boolean，那么以is开头的方法则是更合适的方法
     * 3.冲突方法的返回值类型相同，但返回值类型非boolean，此时出现歧义，抛出异常
     * 4.冲突方法的返回值类型不相关，无法确定哪个是更好的选择，此时直接抛异常
     */
    private void resolveGetterConflicts(Map<String, List<Method>> conflictingGetters) {
        for (Entry<String, List<Method>> entry : conflictingGetters.entrySet()) {
            Method winner = null;
            String propName = entry.getKey();
            for (Method candidate : entry.getValue()) {
                if (winner == null) {
                    winner = candidate;
                    continue;
                }
                // 获取返回值类型
                Class<?> winnerType = winner.getReturnType();
                Class<?> candidateType = candidate.getReturnType();
                /*
                 * 两个方法的返回值类型一致，若两个方法返回值类型均为 boolean，则选取 isXXX 方法
                 * 为 winner。否则无法决定哪个方法更为合适，只能抛出异常
                 */
                if (candidateType.equals(winnerType)) {
                    if (!boolean.class.equals(candidateType)) {
                        throw new ReflectionException(
                                "Illegal overloaded getter method with ambiguous type for property "
                                        + propName + " in class " + winner.getDeclaringClass()
                                        + ". This breaks the JavaBeans specification and can cause unpredictable results.");
                    } else if (candidate.getName().startsWith("is")) {
                        /*
                         * 如果方法返回值类型为 boolean，且方法名以 "is" 开头，
                         * 则认为候选方法 candidate 更为合适
                         */
                        winner = candidate;
                    }
                } else if (candidateType.isAssignableFrom(winnerType)) {
                    // OK getter type is descendant
                    /*
                     * winnerType 是 candidateType 的子类，类型上更为具体，
                     * 则认为当前的 winner 仍是合适的，无需做什么事情
                     */
                } else if (winnerType.isAssignableFrom(candidateType)) {
                    /*
                     * candidateType 是 winnerType 的子类，此时认为 candidate 方法更为合适，
                     * 故将 winner 更新为 candidate
                     */
                    winner = candidate;
                } else {
                    throw new ReflectionException(
                            "Illegal overloaded getter method with ambiguous type for property "
                                    + propName + " in class " + winner.getDeclaringClass()
                                    + ". This breaks the JavaBeans specification and can cause unpredictable results.");
                }
            } // end for
            // 将筛选出的方法添加到 getMethods 中，并将方法返回值添加到 getTypes 中
            addGetMethod(propName, winner);
        }
    }

    private void addGetMethod(String name, Method method) {
        if (isValidPropertyName(name)) {
            getMethods.put(name, new MethodInvoker(method));
            // 解析返回值类型
            Type returnType = TypeParameterResolver.resolveReturnType(method, type);
            // 将返回值类型由 Type 转为 Class，并将转换后的结果缓存到 setTypes 中
            getTypes.put(name, typeToClass(returnType));
            // System.out.println(method.getName());
        }
    }

    private void addSetMethods(Class<?> cls) {
        Map<String, List<Method>> conflictingSetters = new HashMap<String, List<Method>>();
        // 获取当前类，接口，以及父类中的方法。
        Method[] methods = getClassMethods(cls);
        for (Method method : methods) {
            String name = method.getName();
            // 过滤出 setter 方法，且方法仅有一个参数
            if (name.startsWith("set") && name.length() > 3) {
                if (method.getParameterTypes().length == 1) {
                    name = PropertyNamer.methodToProperty(name);
                    /*
                     * setter 方法发生冲突原因是：可能存在重载情况，比如：
                     *     void setSex(int sex);
                     *     void setSex(SexEnum sex);
                     */
                    addMethodConflict(conflictingSetters, name, method);
                }
            }
        }
        // 解决 setter 冲突
        resolveSetterConflicts(conflictingSetters);
    }

    /**
     * 关于 setter 方法冲突的解析规则，这里也总结一下吧。如下：
     * <p>
     * 1.冲突方法的参数类型与 getter 的返回类型一致，则认为是最好的选择
     * 2.冲突方法的参数类型具有继承关系，子类参数对应的方法被认为是更合适的选择
     * 3.冲突方法的参数类型不相关，无法确定哪个是更好的选择，此时直接抛异常
     */
    private void resolveSetterConflicts(Map<String, List<Method>> conflictingSetters) {
        for (String propName : conflictingSetters.keySet()) {
            List<Method> setters = conflictingSetters.get(propName);
            // 获取该属性get方法的返回值Class<?>对象
            /*
             * 获取 getter 方法的返回值类型，由于 getter 方法不存在重载的情况，
             * 所以可以用它的返回值类型反推哪个 setter 的更为合适
             */
            Class<?> getterType = getTypes.get(propName);
            Method match = null;
            ReflectionException exception = null;
            for (Method setter : setters) {
                // 获取参数类型
                Class<?> paramType = setter.getParameterTypes()[0];
                if (paramType.equals(getterType)) {
                    // should be the best match
                    // 参数类型和返回类型一致，则认为是最好的选择，并结束循环
                    match = setter;
                    break;
                }
                if (exception == null) {
                    try {
                        // 选择一个更为合适的方法
                        match = pickBetterSetter(match, setter, propName);
                    } catch (ReflectionException e) {
                        // there could still be the 'best match'
                        match = null;
                        exception = e;
                    }
                }
            }
            if (match == null) {
                // 若 match 为空，表示没找到更为合适的方法，此时抛出异常
                throw exception;
            } else {
                // 将筛选出的方法放入 setMethods 中，并将方法参数值添加到 setTypes 中
                addSetMethod(propName, match);
            }
        }
    }

    /**
     * 从两个 setter 方法中选择一个更为合适方法
     */
    private Method pickBetterSetter(Method setter1, Method setter2, String property) {
        if (setter1 == null) {
            return setter2;
        }
        // Object.class 、String.class
        Class<?> paramType1 = setter1.getParameterTypes()[0];
        Class<?> paramType2 = setter2.getParameterTypes()[0];
        // 如果参数2可赋值给参数1，即参数2是参数1的子类，则认为参数2对应的 setter 方法更为合适
        if (paramType1.isAssignableFrom(paramType2)) {
            return setter2;
        } else if (paramType2.isAssignableFrom(paramType1)) {
            return setter1;
        }
        // 两种参数类型不相关，这里抛出异常
        throw new ReflectionException("Ambiguous setters defined for property '" + property + "' in class '"
                + setter2.getDeclaringClass() + "' with types '" + paramType1.getName() + "' and '"
                + paramType2.getName() + "'.");
    }

    private void addSetMethod(String name, Method method) {
        if (isValidPropertyName(name)) {
            setMethods.put(name, new MethodInvoker(method));
            // 解析参数类型列表
            Type[] paramTypes = TypeParameterResolver.resolveParamTypes(method, type);
            // 将参数类型由 Type 转为 Class，并将转换后的结果缓存到 setTypes
            setTypes.put(name, typeToClass(paramTypes[0]));
        }
    }

    private Class<?> typeToClass(Type src) {
        Class<?> result = null;
        if (src instanceof Class) {
            result = (Class<?>) src;
        } else if (src instanceof ParameterizedType) {
            result = (Class<?>) ((ParameterizedType) src).getRawType();
        } else if (src instanceof GenericArrayType) {
            Type componentType = ((GenericArrayType) src).getGenericComponentType();
            if (componentType instanceof Class) {
                result = Array.newInstance((Class<?>) componentType, 0).getClass();
            } else {
                Class<?> componentClass = typeToClass(componentType);
                result = Array.newInstance((Class<?>) componentClass, 0).getClass();
            }
        }
        if (result == null) {
            result = Object.class;
        }
        return result;
    }

    /**
     * 当字段没有对应的getter/setter方法时会添加相应的GetFieldInvoker/SetFieldInvoker对象
     * 所以Reflector有权限访问指定的字段
     *
     * @param clazz
     */
    private void addFields(Class<?> clazz) {
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            if (canAccessPrivateMethods()) {
                try {
                    field.setAccessible(true);
                } catch (Exception e) {
                    // Ignored. This is only a final precaution, nothing we can do.
                }
            }
            if (field.isAccessible()) {
                if (!setMethods.containsKey(field.getName())) {
                    // issue #379 - removed the check for final because JDK 1.5 allows
                    // modification of final fields through reflection (JSR-133). (JGB)
                    // pr #16 - final static can only be set by the classloader
                    int modifiers = field.getModifiers();
                    if (!(Modifier.isFinal(modifiers) && Modifier.isStatic(modifiers))) {
                        addSetField(field);
                    }
                }
                if (!getMethods.containsKey(field.getName())) {
                    addGetField(field);
                }
            }
        }
        if (clazz.getSuperclass() != null) {
            addFields(clazz.getSuperclass());
        }
    }

    private void addSetField(Field field) {
        if (isValidPropertyName(field.getName())) {
            setMethods.put(field.getName(), new SetFieldInvoker(field));
            Type fieldType = TypeParameterResolver.resolveFieldType(field, type);
            setTypes.put(field.getName(), typeToClass(fieldType));
        }
    }

    private void addGetField(Field field) {
        if (isValidPropertyName(field.getName())) {
            getMethods.put(field.getName(), new GetFieldInvoker(field));
            Type fieldType = TypeParameterResolver.resolveFieldType(field, type);
            getTypes.put(field.getName(), typeToClass(fieldType));
        }
    }

    private boolean isValidPropertyName(String name) {
        return !(name.startsWith("$") || "serialVersionUID".equals(name) || "class".equals(name));
    }

    /**
     * This method returns an array containing all methods
     * declared in this class and any superclass.
     * We use this method, instead of the simpler Class.getMethods(),
     * because we want to look for private methods as well.
     * <p>
     * 获取当前类以及其父类中定义的所有方法的唯一签名以及相应的Method对象。
     *
     * @param cls The class
     * @return An array containing all methods in this class
     */
    private Method[] getClassMethods(Class<?> cls) {
        Map<String, Method> uniqueMethods = new HashMap<String, Method>();
        Class<?> currentClass = cls;
        while (currentClass != null && currentClass != Object.class) {
            addUniqueMethods(uniqueMethods, currentClass.getDeclaredMethods());

            // we also need to look for interface methods -
            // because the class may be abstract
            Class<?>[] interfaces = currentClass.getInterfaces();
            for (Class<?> anInterface : interfaces) {
                addUniqueMethods(uniqueMethods, anInterface.getMethods());
            }

            currentClass = currentClass.getSuperclass();
        }

        Collection<Method> methods = uniqueMethods.values();

        return methods.toArray(new Method[methods.size()]);
    }

    private void addUniqueMethods(Map<String, Method> uniqueMethods, Method[] methods) {
        for (Method currentMethod : methods) {
            // 桥接方法是 JDK 1.5 引入泛型后，为了使Java的泛型方法生成的字节码和 1.5 版本前的字节码相兼容，
            //   由编译器自动生成的方法。
            if (!currentMethod.isBridge()) {
                String signature = getSignature(currentMethod);
                // check to see if the method is already known
                // if it is known, then an extended class must have
                // overridden a method
                if (!uniqueMethods.containsKey(signature)) {
                    if (canAccessPrivateMethods()) {
                        try {
                            currentMethod.setAccessible(true);
                        } catch (Exception e) {
                            // Ignored. This is only a final precaution, nothing we can do.
                        }
                    }

                    uniqueMethods.put(signature, currentMethod);
                }
            } // 非桥接方法
        } // end for
    }

    private String getSignature(Method method) {
        StringBuilder sb = new StringBuilder();
        Class<?> returnType = method.getReturnType();
        if (returnType != null) {
            sb.append(returnType.getName()).append('#');
        }
        sb.append(method.getName());
        Class<?>[] parameters = method.getParameterTypes();
        for (int i = 0; i < parameters.length; i++) {
            if (i == 0) {
                sb.append(':');
            } else {
                sb.append(',');
            }
            sb.append(parameters[i].getName());
        }
        return sb.toString();
    }

    private static boolean canAccessPrivateMethods() {
        try {
            SecurityManager securityManager = System.getSecurityManager();
            if (null != securityManager) {
                securityManager.checkPermission(new ReflectPermission("suppressAccessChecks"));
            }
        } catch (SecurityException e) {
            return false;
        }
        return true;
    }

    /**
     * Gets the name of the class the instance provides information for
     *
     * @return The class name
     */
    public Class<?> getType() {
        return type;
    }

    public Constructor<?> getDefaultConstructor() {
        if (defaultConstructor != null) {
            return defaultConstructor;
        } else {
            throw new ReflectionException("There is no default constructor for " + type);
        }
    }

    public boolean hasDefaultConstructor() {
        return defaultConstructor != null;
    }

    public Invoker getSetInvoker(String propertyName) {
        Invoker method = setMethods.get(propertyName);
        if (method == null) {
            throw new ReflectionException("There is no setter for property named '" + propertyName + "' in '" + type + "'");
        }
        return method;
    }

    public Invoker getGetInvoker(String propertyName) {
        Invoker method = getMethods.get(propertyName);
        if (method == null) {
            throw new ReflectionException("There is no getter for property named '" + propertyName + "' in '" + type + "'");
        }
        return method;
    }

    /**
     * Gets the type for a property setter
     *
     * @param propertyName - the name of the property
     * @return The Class of the propery setter
     */
    public Class<?> getSetterType(String propertyName) {
        Class<?> clazz = setTypes.get(propertyName);
        if (clazz == null) {
            throw new ReflectionException("There is no setter for property named '" + propertyName + "' in '" + type + "'");
        }
        return clazz;
    }

    /**
     * Gets the type for a property getter
     *
     * @param propertyName - the name of the property
     * @return The Class of the propery getter
     */
    public Class<?> getGetterType(String propertyName) {
        Class<?> clazz = getTypes.get(propertyName);
        if (clazz == null) {
            throw new ReflectionException("There is no getter for property named '" + propertyName + "' in '" + type + "'");
        }
        return clazz;
    }

    /**
     * Gets an array of the readable properties for an object
     *
     * @return The array
     */
    public String[] getGetablePropertyNames() {
        return readablePropertyNames;
    }

    /**
     * Gets an array of the writeable properties for an object
     *
     * @return The array
     */
    public String[] getSetablePropertyNames() {
        return writeablePropertyNames;
    }

    /**
     * Check to see if a class has a writeable property by name
     *
     * @param propertyName - the name of the property to check
     * @return True if the object has a writeable property by the name
     */
    public boolean hasSetter(String propertyName) {
        return setMethods.keySet().contains(propertyName);
    }

    /**
     * Check to see if a class has a readable property by name
     *
     * @param propertyName - the name of the property to check
     * @return True if the object has a readable property by the name
     */
    public boolean hasGetter(String propertyName) {
        return getMethods.keySet().contains(propertyName);
    }

    public String findPropertyName(String name) {
        return caseInsensitivePropertyMap.get(name.toUpperCase(Locale.ENGLISH));
    }
}
